package com.renjibo.progressdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.LinearLayout;

/**
 * 任继波
 * Created by Administrator on 2017/6/24.
 */

public class BootScheduleView extends LinearLayout {
    /**
     * 当前进度
     *
     * @param context
     */
    private int progress;
    private Paint mPaint;
    private Context context;
    // 圆点半径 和图片大小
    private int cicleR = 22;

    //选中图片
    Bitmap circleSelectBitmap;

    //默认图片
    Bitmap circleBitmap;

    //圆点 图片的左边距离
    private int myX = 50;

    //默认底层线的颜色
    private int bottomColor;

    //底层线选中的颜色
    private int bottomSelectColor;


    //默认圆点的颜色
    private int cicleColor;

    //选中圆点的颜色
    private int cicleSelectColor;

    //圆点和线的距离
    private int cicleLineMes = 5;

    //文字
    private final String leftName = "实名注册";
    private final String centerName = "关联宝贝";
    private final String rightName = "绑定硬件";
    //第一种状态
    private int count=1;
    private Rect rect;

    public BootScheduleView(Context context) {
        this(context, null);
    }

    public BootScheduleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BootScheduleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        //初始化 状态值
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BootScheduleView);
        count = ta.getInteger(R.styleable.BootScheduleView_countState,1);

        mPaint = new Paint();
        rect = new Rect();
        //设置画笔为无锯齿
        mPaint.setAntiAlias(false);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.check_icon_disable);
        circleBitmap = bitmap;
        Bitmap bitmapSelect = BitmapFactory.decodeResource(getResources(), R.mipmap.check_icon);
        circleSelectBitmap = bitmapSelect;
//        circleBitmap = getCroppedRoundBitmap(bitmap, dip2px(context, cicleR));

        //初始化圆点颜色
        cicleColor = Color.parseColor("#89A0FC");
        cicleSelectColor = Color.parseColor("#7ED321");
        //初始化底线颜色
        bottomColor = Color.parseColor("#FFFFFF");
        bottomSelectColor = Color.parseColor("#7ED321");
    }
    public void setCount(int count){
        this.count=count;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画左边文字
        canvas.save();
        mPaint.setColor(Color.parseColor("#ffffff"));
        mPaint.setTextSize(dip2px(context, 11));
        mPaint.getTextBounds(leftName, 0, leftName.length(), rect);
        int w = rect.width();
        int h = rect.height();
        canvas.drawText(leftName, dip2px(context, myX) - w / 2, getHeight() / 2 - dip2px(context, 4) + dip2px(context, cicleR + 4) + h, mPaint);
        canvas.restore();

        //画中间文字
        canvas.save();
        mPaint.setColor(Color.parseColor("#ffffff"));
        mPaint.setTextSize(dip2px(context, 11));
        mPaint.getTextBounds(centerName, 0, centerName.length(), rect);
        int wCenter = rect.width();
        int hCenter = rect.height();
        canvas.drawText(centerName, getWidth() / 2 - wCenter / 2, getHeight() / 2 - dip2px(context, 4) + dip2px(context, cicleR + 4) + hCenter, mPaint);
        canvas.restore();

        //画右边的文字
        canvas.save();
        mPaint.setColor(Color.parseColor("#ffffff"));
        mPaint.setTextSize(dip2px(context, 11));
        mPaint.getTextBounds(rightName, 0, rightName.length(), rect);
        int wRight = rect.width();
        int hLeft = rect.height();
        canvas.drawText(rightName, getWidth() - dip2px(context, myX) - wRight / 2, getHeight() / 2 - dip2px(context, 4) + dip2px(context, cicleR + 4) + hLeft, mPaint);
        canvas.restore();
        if(count==1){
            oneState(canvas);
        }else if(count==2){
            twoState(canvas);
        }else if(count==3){
            threeState(canvas);
        }else if(count==4){
            fourState(canvas);
        }
//
//
//        canvas.save();
//        mPaint.setColor(Color.BLUE);
//        canvas.drawCircle(getWidth()/2,getHeight()/2,50,mPaint);
//        canvas.restore();
        //最底层全部的线
//        canvas.save();
//        mPaint.setColor(bottomColor);
//        canvas.drawRect(dip2px(context, myX), getHeight() / 2 - 20, getWidth() - dip2px(context, myX), getHeight() / 2 + 20, mPaint);// 长方形
//        canvas.restore();
//        canvas.save();
//        //左边步骤线
//        mPaint.setColor(Color.GREEN);
//        canvas.drawRect(dip2px(context, myX), getHeight() / 2 - 20, getWidth() / 2, getHeight() / 2 + 20, mPaint);// 长方形
//        canvas.restore();
////        //右边步骤线
////        mPaint.setColor(Color.GREEN);
////        canvas.drawRect(getWidth() / 2, getHeight() / 2 - 20, getWidth() - dip2px(context, myX), getHeight() / 2 + 20, mPaint);// 长方形
////        canvas.restore();
//        //中间的圆点
//        canvas.save();
//        mPaint.setColor(Color.RED);
//        canvas.drawCircle(getWidth() / 2, getHeight() / 2, dip2px(context, cicleR), mPaint);
//        canvas.restore();
//        //左边的圆点
//        canvas.save();
//        mPaint.setColor(Color.RED);
//        canvas.drawCircle(dip2px(context, myX), getHeight() / 2, dip2px(context, cicleR), mPaint);
//        canvas.restore();
//        //右边的圆点
//        canvas.save();
//        mPaint.setColor(Color.RED);
//        canvas.drawCircle(getWidth() - dip2px(context, myX), getHeight() / 2, dip2px(context, cicleR), mPaint);
//        canvas.restore();
//
//        //左边的图片  20
//
//        canvas.save();
//        mPaint.setColor(Color.RED);
//        canvas.drawBitmap(circleBitmap, dip2px(context, myX) - circleBitmap.getWidth() / 2, getHeight() / 2 - circleBitmap.getHeight() / 2, mPaint);
//        canvas.restore();
//        //中间的图片  20
//
//        canvas.save();
//        mPaint.setColor(Color.RED);
//        canvas.drawBitmap(circleBitmap, getWidth() / 2 - circleBitmap.getWidth() / 2, getHeight() / 2 - circleBitmap.getHeight() / 2, mPaint);
//        canvas.restore();
//        //右边的图片  20
//        canvas.save();
//        mPaint.setColor(Color.RED);
//        canvas.drawBitmap(circleBitmap,getWidth()- dip2px(context, myX)-circleBitmap.getWidth()/2, getHeight() / 2 - circleBitmap.getHeight() / 2, mPaint);
//        canvas.restore();


    }

    /**
     * 第四种状态
     */
    private void fourState(Canvas canvas) {
        //左边步骤线
        canvas.save();
        mPaint.setColor(bottomSelectColor);
        canvas.drawRect(dip2px(context, myX) + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() / 2 - dip2px(context, cicleR + cicleLineMes), dip2px(context, 41), mPaint);// 长方形
        canvas.restore();

        //右边步骤线
        canvas.save();
        mPaint.setColor(bottomSelectColor);
//        canvas.drawRect(getWidth() / 2 + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() - dip2px(context, cicleR + cicleLineMes) * 3, dip2px(context, 41), mPaint);// 长方形
        canvas.drawRect(getWidth() / 2 + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() - dip2px(context, myX + cicleLineMes) - cicleR * 2, dip2px(context, 41), mPaint);// 长方形
        canvas.restore();

//        //中间的圆点
//        canvas.save();
//        mPaint.setColor(cicleSelectColor);
//        canvas.drawCircle(getWidth() / 2, getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
//        canvas.restore();

//        //左边的圆点
//        canvas.save();
//        mPaint.setColor(cicleSelectColor);
//        canvas.drawCircle(dip2px(context, myX), getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
//        canvas.restore();

        //右边的圆点
        canvas.save();
        mPaint.setColor(cicleSelectColor);
        canvas.drawCircle(getWidth() - dip2px(context, myX), getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
        canvas.restore();

        //左边的图片  20
        canvas.save();
        mPaint.setColor(Color.RED);
        canvas.drawBitmap(circleSelectBitmap, dip2px(context, myX) - circleBitmap.getWidth() / 2, getHeight() / 2 - dip2px(context, 4) - circleBitmap.getHeight() / 2, mPaint);
        canvas.restore();


        //中间的图片  20
        canvas.save();
        mPaint.setColor(Color.RED);
        canvas.drawBitmap(circleSelectBitmap, getWidth() / 2 - circleBitmap.getWidth() / 2, getHeight() / 2 - dip2px(context, 4) - circleBitmap.getHeight() / 2, mPaint);
        canvas.restore();

    }

    /**
     * 第三种状态
     */
    private void threeState(Canvas canvas) {
        //左边步骤线
        canvas.save();
        mPaint.setColor(bottomSelectColor);
        canvas.drawRect(dip2px(context, myX) + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() / 2 - dip2px(context, cicleR + cicleLineMes), dip2px(context, 41), mPaint);// 长方形
        canvas.restore();

        //右边步骤线
        canvas.save();
        mPaint.setColor(bottomColor);
//        canvas.drawRect(getWidth() / 2 + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() - dip2px(context, cicleR + cicleLineMes) * 3, dip2px(context, 41), mPaint);// 长方形
        canvas.drawRect(getWidth() / 2 + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() - dip2px(context, myX + cicleLineMes) - cicleR * 2, dip2px(context, 41), mPaint);// 长方形
        canvas.restore();

//        //中间的圆点
//        canvas.save();
//        mPaint.setColor(cicleSelectColor);
//        canvas.drawCircle(getWidth() / 2, getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
//        canvas.restore();

//        //左边的圆点
//        canvas.save();
//        mPaint.setColor(cicleSelectColor);
//        canvas.drawCircle(dip2px(context, myX), getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
//        canvas.restore();

        //右边的圆点
        canvas.save();
        mPaint.setColor(cicleColor);
        canvas.drawCircle(getWidth() - dip2px(context, myX), getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
        canvas.restore();

        //左边的图片  20
        canvas.save();
        mPaint.setColor(Color.RED);
        canvas.drawBitmap(circleSelectBitmap, dip2px(context, myX) - circleBitmap.getWidth() / 2, getHeight() / 2 - dip2px(context, 4) - circleBitmap.getHeight() / 2, mPaint);
        canvas.restore();


        //中间的图片  20
        canvas.save();
        mPaint.setColor(Color.RED);
        canvas.drawBitmap(circleSelectBitmap, getWidth() / 2 - circleBitmap.getWidth() / 2, getHeight() / 2 - dip2px(context, 4) - circleBitmap.getHeight() / 2, mPaint);
        canvas.restore();

    }

    /**
     * 第二种状态
     */
    private void twoState(Canvas canvas) {
        //左边步骤线
        canvas.save();
        mPaint.setColor(bottomSelectColor);
        canvas.drawRect(dip2px(context, myX) + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() / 2 - dip2px(context, cicleR + cicleLineMes), dip2px(context, 41), mPaint);// 长方形
        canvas.restore();

        //右边步骤线
        canvas.save();
        mPaint.setColor(bottomColor);
//        canvas.drawRect(getWidth() / 2 + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() - dip2px(context, cicleR + cicleLineMes) * 3, dip2px(context, 41), mPaint);// 长方形
        canvas.drawRect(getWidth() / 2 + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() - dip2px(context, myX + cicleLineMes) - cicleR * 2, dip2px(context, 41), mPaint);// 长方形

        canvas.restore();

        //中间的圆点
        canvas.save();
        mPaint.setColor(cicleSelectColor);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
        canvas.restore();

//        //左边的圆点
//        canvas.save();
//        mPaint.setColor(cicleSelectColor);
//        canvas.drawCircle(dip2px(context, myX), getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
//        canvas.restore();

        //右边的圆点
        canvas.save();
        mPaint.setColor(cicleColor);
        canvas.drawCircle(getWidth() - dip2px(context, myX), getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
        canvas.restore();

        //左边的图片  20
        canvas.save();
        mPaint.setColor(Color.RED);
        canvas.drawBitmap(circleSelectBitmap, dip2px(context, myX) - circleBitmap.getWidth() / 2, getHeight() / 2 - dip2px(context, 4) - circleBitmap.getHeight() / 2, mPaint);
        canvas.restore();

//        //中间的图片  20
//        canvas.save();
//        mPaint.setColor(Color.RED);
//        canvas.drawBitmap(circleBitmap, getWidth() / 2 - circleBitmap.getWidth() / 2, getHeight() / 2 - dip2px(context, 4) - circleBitmap.getHeight() / 2, mPaint);
//        canvas.restore();

    }

    /**
     * 第一种状态
     */
    private void oneState(Canvas canvas) {
//        //最底层全部的线
//        canvas.save();
//        mPaint.setColor(bottomColor);
//        canvas.drawRect(dip2px(context, myX), getHeight() / 2 - 20, getWidth() - dip2px(context, myX), getHeight() / 2 + 20, mPaint);// 长方形
//        canvas.restore();
//        canvas.save();
//        rect = new Rect();
//        //画左边文字
//        canvas.save();
//        mPaint.setColor(Color.parseColor("#ffffff"));
//        mPaint.setTextSize(dip2px(context, 11));
//        mPaint.getTextBounds(leftName, 0, leftName.length(), rect);
//        int w = rect.width();
//        int h = rect.height();
//        canvas.drawText(leftName, dip2px(context, myX) - w / 2, getHeight() / 2 - dip2px(context, 4) + dip2px(context, cicleR + 4) + h, mPaint);
//        canvas.restore();
//
//        //画中间文字
//        canvas.save();
//        mPaint.setColor(Color.parseColor("#ffffff"));
//        mPaint.setTextSize(dip2px(context, 11));
//        mPaint.getTextBounds(centerName, 0, centerName.length(), rect);
//        int wCenter = rect.width();
//        int hCenter = rect.height();
//        canvas.drawText(centerName, getWidth() / 2 - wCenter / 2, getHeight() / 2 - dip2px(context, 4) + dip2px(context, cicleR + 4) + hCenter, mPaint);
//        canvas.restore();
//
//        //画右边的文字
//        canvas.save();
//        mPaint.setColor(Color.parseColor("#ffffff"));
//        mPaint.setTextSize(dip2px(context, 11));
//        mPaint.getTextBounds(rightName, 0, rightName.length(), rect);
//        int wRight = rect.width();
//        int hLeft = rect.height();
//        canvas.drawText(rightName, getWidth() - dip2px(context, myX) - wRight / 2, getHeight() / 2 - dip2px(context, 4) + dip2px(context, cicleR + 4) + hLeft, mPaint);
//        canvas.restore();

        //左边步骤线
        canvas.save();
        mPaint.setColor(bottomColor);
        canvas.drawRect(dip2px(context, myX) + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() / 2 - dip2px(context, cicleR + cicleLineMes), dip2px(context, 41), mPaint);// 长方形
        canvas.restore();


        //中间的圆点
        canvas.save();
        mPaint.setColor(cicleColor);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
        canvas.restore();

        //左边的圆点
        canvas.save();
        mPaint.setColor(cicleSelectColor);
        canvas.drawCircle(dip2px(context, myX), getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
        canvas.restore();

        //右边的圆点
        canvas.save();
        mPaint.setColor(cicleColor);
        canvas.drawCircle(getWidth() - dip2px(context, myX), getHeight() / 2 - dip2px(context, 4), dip2px(context, cicleR), mPaint);
        canvas.restore();

        //右边步骤线
        canvas.save();
        mPaint.setColor(bottomColor);
//        canvas.drawRect(getWidth() / 2 + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() -dip2px(context, cicleR + cicleLineMes) * 3, dip2px(context, 41), mPaint);// 长方形
        canvas.drawRect(getWidth() / 2 + dip2px(context, cicleR + cicleLineMes), dip2px(context, 33), getWidth() - dip2px(context, myX + cicleLineMes) - cicleR * 2, dip2px(context, 41), mPaint);// 长方形
        canvas.restore();

//        // 画符号
//        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        mPaint.setColor(getResources().getColor(android.R.color.white));
//        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setStrokeCap(Paint.Cap.ROUND);
//        mPaint.setStrokeJoin(Paint.Join.ROUND);
//        canvas.drawLine(start.x, start.y, end.x, end.y, mSymbolPaint);
//        /**
//         * 绘制对勾
//         */
//        //获取圆心的x坐标
//        int center = getWidth() / 2;
//        int center1 = center - getWidth() / 5;
//            //画第一根线
//            canvas.drawLine(center1, center, center1 + line1_x, center + line1_y, paint);
//
//            //画第二根线
//            canvas.drawLine(center1 + line1_x - 1, center + line1_y, center1 + line2_x, center + line2_y, paint);
    }

    /**
     * 获取裁剪后的圆形图片
     *
     * @param
     */
//    radius半径
    public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
        Bitmap scaledSrcBmp;
        int diameter = radius * 2;
        // 为了防止宽高不相等，造成圆形图片变形，因此截取长方形中处于中间位置最大的正方形图片
        int bmpWidth = bmp.getWidth();
        int bmpHeight = bmp.getHeight();
        int squareWidth = 0, squareHeight = 0;
        int x = 0, y = 0;
        Bitmap squareBitmap;
        if (bmpHeight > bmpWidth) {// 高大于宽
            squareWidth = squareHeight = bmpWidth;
            x = 0;
            y = (bmpHeight - bmpWidth) / 2;
            // 截取正方形图片
            squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
        } else if (bmpHeight < bmpWidth) {// 宽大于高
            squareWidth = squareHeight = bmpHeight;
            x = (bmpWidth - bmpHeight) / 2;
            y = 0;
            squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
        } else {
            squareBitmap = bmp;
        }
        if (squareBitmap.getWidth() != diameter || squareBitmap.getHeight() != diameter) {
            scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter, diameter, true);
        } else {
            scaledSrcBmp = squareBitmap;
        }
        Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(),
                scaledSrcBmp.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawCircle(scaledSrcBmp.getWidth() / 2,
                scaledSrcBmp.getHeight() / 2,
                scaledSrcBmp.getWidth() / 2,
                paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
        return output;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
